package com.zhuoyue.biz.core.command.impl;

import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhuoyue.biz.core.command.Device;
import com.zhuoyue.biz.core.command.constant.GatewayCmdDeal;
import com.zhuoyue.common.utils.CmdUtils;
/**
 * @ClassName: Gateway
 * @Description:
 * @author wjming
 * @Date 2021-07-14 20:21
 */
public class Gateway implements Device {
	
	

	@Override
	public String getCommand(String controlParam) {
		Map<String, String> map = (Map)JSONObject.parse(controlParam);
		
		String controlType = map.get("controlType");
		
		String cmd = "";
		
		if(GatewayCmdDeal.DealType.REFRESH_REPORT.equals(controlType)){
			cmd = getRefreshReportCmd(map);
		}else if(GatewayCmdDeal.DealType.PARAM_READ.equals(controlType)){
			cmd = getParamReadCmd(map);
		}else if(GatewayCmdDeal.DealType.GATEWAY_ENABLE.equals(controlType)){
			cmd = getGatewayEnableCmd(map);
		}else if(GatewayCmdDeal.DealType.GATEWAY_DISABLE.equals(controlType)){
			cmd = getGatewayDisableCmd(map);
		}else if(GatewayCmdDeal.DealType.GATEWAY_DEFEND.equals(controlType)){
			cmd = getGatewayDefendCmd(map);
		}else if(GatewayCmdDeal.DealType.READ_VERSION.equals(controlType)){
			cmd = getReadVersionCmd(map);
		}
		
		return cmd;
	}
	
	private String getGatewayEnableCmd(Map<String , String> map){
		
		String gatewayMac = map.get("gatewayMac");
		
		String cmd3To7 = GatewayCmdDeal.GatewayEnable.CMD_3 +
							GatewayCmdDeal.GatewayEnable.CMD_4 +
							gatewayMac + 
							GatewayCmdDeal.GatewayEnable.CMD_6 +
							GatewayCmdDeal.GatewayEnable.CMD_7;
		
		String cmd2 = CmdUtils.getDateLength(cmd3To7);
		
		String cmd8 = CmdUtils.getCmdBodySum(cmd3To7);
		
		String fullCmd = GatewayCmdDeal.GatewayEnable.CMD_1 +
						  cmd2 +
						  cmd3To7 +
						  cmd8 +
				          GatewayCmdDeal.GatewayEnable.CMD_9;
		
		return fullCmd;
	}
	
	private String getGatewayDisableCmd(Map<String , String> map){
		
		String gatewayMac = map.get("gatewayMac");
		
		String cmd3To7 = GatewayCmdDeal.GatewayDisable.CMD_3 +
							GatewayCmdDeal.GatewayDisable.CMD_4 +
							gatewayMac + 
							GatewayCmdDeal.GatewayDisable.CMD_6 +
							GatewayCmdDeal.GatewayDisable.CMD_7;

		String cmd2 = CmdUtils.getDateLength(cmd3To7);
		
		String cmd8 = CmdUtils.getCmdBodySum(cmd3To7);
		
		String fullCmd = GatewayCmdDeal.GatewayDisable.CMD_1 +
					  	  cmd2 +
					      cmd3To7 +
					      cmd8 +
			              GatewayCmdDeal.GatewayDisable.CMD_9;
		
		return fullCmd;
		
	}
	
	
	private String getParamReadCmd(Map<String , String> map){
		
		String gatewayMac = map.get("gatewayMac");
		String deviceMac = map.get("deviceMac");
		
		String cmd3To8 = GatewayCmdDeal.ParamRead.CMD_3 +
							GatewayCmdDeal.ParamRead.CMD_4 +
							gatewayMac + 
							deviceMac + 
							GatewayCmdDeal.ParamRead.CMD_7 +
							GatewayCmdDeal.ParamRead.CMD_8;
		
		String cmd2 = CmdUtils.getDateLength(cmd3To8);
		
		String cmd9 = CmdUtils.getCmdBodySum(cmd3To8);
		
		String fullCmd = GatewayCmdDeal.ParamRead.CMD_1 +
						  cmd2 +
						  cmd3To8 +
						  cmd9 +
						  GatewayCmdDeal.ParamRead.CMD_10;
		
		return fullCmd;
	}
	
	
	private String getReadVersionCmd(Map<String , String> map){
		
		String gatewayMac = map.get("gatewayMac");
		String deviceMac = map.get("deviceMac");
		
		String cmd3To8 = GatewayCmdDeal.ReadVersion.CMD_3 +
							GatewayCmdDeal.ReadVersion.CMD_4 +
							gatewayMac + 
							deviceMac + 
							GatewayCmdDeal.ReadVersion.CMD_7 +
							GatewayCmdDeal.ReadVersion.CMD_8;
		
		String cmd2 = CmdUtils.getDateLength(cmd3To8);
		
		String cmd9 = CmdUtils.getCmdBodySum(cmd3To8);
		
		String fullCmd = GatewayCmdDeal.ReadVersion.CMD_1 +
						  cmd2 +
						  cmd3To8 +
						  cmd9 +
						  GatewayCmdDeal.ReadVersion.CMD_10;
		
		return fullCmd;
	}
	
	
	private String getRefreshReportCmd(Map<String , String> map){
		
		String gatewayMac = map.get("gatewayMac");
		
		String cmd3To7 = GatewayCmdDeal.RefreshReport.CMD_3 +
							GatewayCmdDeal.RefreshReport.CMD_4 +
							gatewayMac + 
							GatewayCmdDeal.RefreshReport.CMD_6 +
							GatewayCmdDeal.RefreshReport.CMD_7;
		
		String cmd2 = CmdUtils.getDateLength(cmd3To7);
		
		String cmd8 = CmdUtils.getCmdBodySum(cmd3To7);
		
		String fullCmd = GatewayCmdDeal.RefreshReport.CMD_1 +
						  cmd2 +
						  cmd3To7 +
						  cmd8 +
						  GatewayCmdDeal.RefreshReport.CMD_9;
		
		return fullCmd;
	}
	
	/**
	 * 布防、撤防
	 * 
	 * 发送：2A 0D 50 00 FD A3 C6 0A 00 6F 0D 00 02 05 01 44 23
	 * 
	 * 2A：帧头 
	 * 0D：数据长度 
	 * 50：命令标识 
	 * 00：命令对象 
	 * FD A3 C6 0A 00 6F 0D 00：网关MAC地址 
	 * 02：数据长度
	 * 05：子命令标识，设防类型配置命令 
	 * 01：设防类型为布防，01为布防，02为撤防 
	 * 44：从命令标识处开始的校验和 
	 * 23：帧尾
	 * 
	 * @param map
	 * @return
	 *
	 */
	private String getGatewayDefendCmd(Map<String , String> map){
		
		String gatewayMac = map.get("gatewayMac");
		Map<String , String> actionsMap = (Map)JSONObject.parse(JSON.toJSONString(map.get("actions")));
		String controlState = actionsMap.get("controlState");
		
		String cmd3To8 = GatewayCmdDeal.GatewayDefend.CMD_3 +
							GatewayCmdDeal.GatewayDefend.CMD_4 +
							gatewayMac + 
							GatewayCmdDeal.GatewayDefend.CMD_6 +
							GatewayCmdDeal.GatewayDefend.CMD_7 +
							controlState;

		String cmd2 = CmdUtils.getDateLength(cmd3To8);
		
		String cmd9 = CmdUtils.getCmdBodySum(cmd3To8);
		
		String fullCmd = GatewayCmdDeal.GatewayDefend.CMD_1 +
					  	  cmd2 +
					      cmd3To8 +
					      cmd9 +
			              GatewayCmdDeal.GatewayDefend.CMD_10;
		
		return fullCmd;
		
	}

}
